package com.guard.net;

import android.os.AsyncTask;

import com.guard.structures.ProgressObserver;

public abstract class PeriodicUpdateTask extends AsyncTask<Void, Void, Object> {
    private ProgressObserver mObserver;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Object doInBackground(Void... params) {
        Object obj = doSomething();
        return obj;
    }

    public abstract Object doSomething();

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(Object obj) {
        super.onPostExecute(obj);

        if (obj != null) {
            // Update current status
        }

        if (mObserver != null)
            mObserver.onFinished(obj);
    }

    public void registerObserver(ProgressObserver observer) {
        mObserver = observer;
    }

    public void unRegisterObserver(ProgressObserver observer) {
        if (mObserver == observer)
            mObserver = null;
    }

}
